using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node declaration_specifiers in the grammar(AnsiC).
    /// </summary>
    internal partial class Vndeclaration_specifiers : IFullFormatter {
        // [076]=declaration_specifiers : storage_class_specifier ;
        // [077]=declaration_specifiers : storage_class_specifier declaration_specifiers ;
        // [078]=declaration_specifiers : type_specifier ;
        // [079]=declaration_specifiers : type_specifier declaration_specifiers ;
        // [080]=declaration_specifiers : type_qualifier ;
        // [081]=declaration_specifiers : type_qualifier declaration_specifiers ;


        private readonly IFullFormatter last0;
        private readonly List<IFullFormatter> list0 = new();

        private readonly TokenRange _scope;

        public TokenRange Scope => _scope;

        public void Format(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }

        public void FullFormat(BlankConfig preConfig, TextWriter writer, FormatContext context) {
            var count = this.list0.Count;
            var config = new BlankConfig(inlineBlank: 1, forceNewline: false);

            for (var i = 0; i < count; i++) {
                var item0 = this.list0[i];
                context.PrintBlanksAnd(item0, i == 0 ? preConfig : config, writer);
                if (i + 1 < count) {
                    context.PrintCommentsBetween(item0, this.list0[i + 1], config, writer);
                }
                else {
                    context.PrintCommentsBetween(item0, this.last0, config, writer);
                }
            }
            {
                context.PrintBlanksAnd(this.last0, count > 0 ? config : preConfig, writer);
            }
        }

        public IEnumerable<string> YieldTokens(TextWriter writer, FormatContext context) {
            throw new NotImplementedException();
        }
        // [76]=declaration_specifiers : storage_class_specifier ;
        // dumped by ReversedListExtractor 1
        internal Vndeclaration_specifiers(Vnstorage_class_specifier last0) {
            this.last0 = last0;
            this._scope = new TokenRange(last0);
        }
        // [77]=declaration_specifiers : storage_class_specifier declaration_specifiers ;
        // dumped by ReversedListExtractor 2
        internal void Insert(Vnstorage_class_specifier r1) {
            this.list0.Insert(0, r1);
            this._scope.start = r1.Scope.start;
        }
        // [78]=declaration_specifiers : type_specifier ;
        // dumped by ReversedListExtractor 1
        internal Vndeclaration_specifiers(Vntype_specifier last0) {
            this.last0 = last0;
            this._scope = new TokenRange(last0);
        }
        // [79]=declaration_specifiers : type_specifier declaration_specifiers ;
        // dumped by ReversedListExtractor 2
        internal void Insert(Vntype_specifier r1) {
            this.list0.Insert(0, r1);
            this._scope.start = r1.Scope.start;
        }
        // [80]=declaration_specifiers : type_qualifier ;
        // dumped by ReversedListExtractor 1
        internal Vndeclaration_specifiers(Vntype_qualifier last0) {
            this.last0 = last0;
            this._scope = new TokenRange(last0);
        }
        // [81]=declaration_specifiers : type_qualifier declaration_specifiers ;
        // dumped by ReversedListExtractor 2
        internal void Insert(Vntype_qualifier r1) {
            this.list0.Insert(0, r1);
            this._scope.start = r1.Scope.start;
        }

    }
}
